package com.singerw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.singerw.mapper.TwTeamMapper;
import com.singerw.pojo.Permissions;
import com.singerw.pojo.TwTeam;
import com.singerw.pojo.TwUser;
import com.singerw.service.security.TwCollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version V1.0
 * @Package com.singerw.service
 * @auhter 张欣
 * @date 2020/12/30-4:06 PM
 * todo 对应接口需要加入对应权限
 */

@Service
public class TwTeamService {

    @Resource
    private TwTeamMapper twTeamMapper;

    @Autowired
    private TwUserService twUserService;
    @Autowired
    private TwCollegeService twCollegeService;
    public List<TwTeam> findByUserName(String userName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        return twTeamMapper.selectList(wrapper);
    }
    public List<TwTeam> findByUserNameAndTeamName(String userName,String teamName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        wrapper.eq("team_name", teamName);
        return twTeamMapper.selectList(wrapper);
    }

    public List<TwTeam> findByUserNameAndIsManage(String userName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName).and(w -> {
            w.eq("team_role", "teacher").or().eq("team_role", "monitor");
        });
        return twTeamMapper.selectList(wrapper);
    }

    public List<TwTeam> findByTeamName(String teamName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("team_name", teamName);
        return twTeamMapper.selectList(wrapper);
    }

    public List<TwTeam> findByTeamRole(String teamName, String teamRole) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("team_name", teamName).and(w -> {
            w.eq("team_role", teamRole);
        });
        return twTeamMapper.selectList(wrapper);
    }

    public void save(TwTeam twTeam) {
        if (twTeam.getId() == null) {
            twTeamMapper.insert(twTeam);
            //如果不存在此用户，则为其创建，默认密码为 123456
            twUserService.createUser(twTeam.getUserName());
            //如果不存在此学院，则为其创建
            twCollegeService.isExistence(twTeam.getCollegeName());

        } else {
            List<TwTeam> list = findByUserNameAndTeamName(twTeam.getUserName(), twTeam.getTeamName());
            String teamId = twTeam.getId();
            twTeam.setId(null);
            if (list.size() > 0){
                UpdateWrapper<TwTeam> wrapper = new UpdateWrapper<>();
                wrapper.eq("user_name",twTeam.getUserName());
                twTeamMapper.update(twTeam,wrapper);
            }else{
                twTeamMapper.insert(twTeam);
            }
            removeByTeamId(teamId);

        }
        //如果此次更新/插入的目标角色不是学生，那么就赋予其管理权限
        if(!twTeam.getTeamRole().equals("student")){
            twUserService.setPermissions(twTeam.getUserName(),Permissions.TEAM_MANAGE);
        }



    }

    public void removeByTeamName(String teamName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("team_name", teamName);
        twTeamMapper.delete(wrapper);
    }
    public void removeByTeamId(String id) {
        twTeamMapper.deleteById(id);
    }

    /**
     * 将对应用户名的用户从对应的 team 中删除
     *
     * @param userName
     * @param teamName
     */
    public void removeByUserNameFromTeam(String userName, String teamName) {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        wrapper.eq("team_name", teamName);
        twTeamMapper.delete(wrapper);
    }


    public List<TwTeam> list() {
        return twTeamMapper.selectList(null);
    }

    public List<TwTeam> TeamList() {
        QueryWrapper<TwTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("team_role", "admin");
        return twTeamMapper.selectList(wrapper);
    }
}
